﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace JasLib.Interop.Win32
{
    public static class OLE32
    {
        /// <summary>
        /// Creates a single uninitialized object of the class associated with a specified CLSID.
        /// </summary>
        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoCreateInstance", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HRESULT CoCreateInstance(
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
            [In] IUnknown pUnkOuter,
            [In] CLSCTX dwClsContext,
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid riid,
            [Out, MarshalAs(UnmanagedType.Interface)] out object ppv);

        /// <summary>
        /// Creates an instance of a specific class on a specific computer.
        /// </summary>
        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoCreateInstanceEx", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HRESULT CoCreateInstanceEx(
            [In, MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
            [In] IUnknown pUnkOuter,
            [In] CLSCTX dwClsContext,
            [In] ref COSERVERINFO pServerInfo,
            [In] uint dwCount,
            [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 4)] MULTI_QI[] pResults);

        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoInitialize", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HRESULT CoInitialize(UINT_PTR pvReserved);

        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoInitializeEx", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern HRESULT CoInitializeEx(
            [In] UINT_PTR pvReserved,
            [In] COINIT dwCoInit = COINIT.COINIT_APARTMENTTHREADED);

        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoTaskMemAlloc", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern UINT_PTR CoTaskMemAlloc(UINT_PTR cb);

        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoTaskMemFree", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern void CoTaskMemFree(UINT_PTR pv);

        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoTaskMemRealloc", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern UINT_PTR CoTaskMemRealloc(UINT_PTR pv, UINT_PTR cb);

        [DllImport(Constants.ModuleNames.OLE32, EntryPoint = "CoUninitialize", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi, SetLastError = true)]
        public static extern void CoUninitialize();
    }
}
